package au.com.cgu.RescueUTest;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import au.com.cgu.RescueU.MongoBackend;
import au.com.cgu.RescueU.Registration;
import au.com.cgu.RescueU.Vehicle;

public class TestMongoDB {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		MongoBackend.resetDB();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testAdd() {
		Registration reg = new Registration("Michael Gu", "P1234");
		if (reg.RegistrationID() != null) {
			try {
				MongoBackend.store_event(reg);
				System.out.println("Stored: " + reg.RegistrationID());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				fail(e.getMessage());
			}
		} else {
			fail("Didn't generate Registration");
		}
	}
	
	@Test
	public void testFind() {
		Registration reg = new Registration("Michael Gu", "P1234");
		if (reg.RegistrationID() != null) {
			try {
				MongoBackend.store_event(reg);
				System.out.println("Stored: " + reg.RegistrationID());
				if (MongoBackend.find_registration(reg.getID()) == null) {
					fail("Didn't find registration");
				} else {
					System.out.println("Found reg: " + reg.getID());
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				fail(e.getMessage());
			}
		} else {
			fail("Didn't generate Registration");
		}
	}
	
	@Test
	public void testVehicleAdd() {
		Vehicle veh1 = new Vehicle("Suburu WRX STI","ABC123", "P1234");
		Vehicle veh2 = new Vehicle("Suburu WRX","ABC124", "P1234");
		Vehicle veh3 = new Vehicle("Suburu RX","ABC125", "P1234");
		Vehicle veh4 = new Vehicle("Toyota Prius","TKI224", "P1234");
		Vehicle veh5 = new Vehicle("Mercedes Benz SLK 300","IROLLD", "P1234");
		Vehicle veh6 = new Vehicle("Lamborgini Gallardo","TOOGOD", "P1234");
		try {
			MongoBackend.store_event(veh1);
			System.out.println("Stored: " + veh1.getRegistrationNumber());
			MongoBackend.store_event(veh2);
			System.out.println("Stored: " + veh2.getRegistrationNumber());
			MongoBackend.store_event(veh3);
			System.out.println("Stored: " + veh3.getRegistrationNumber());
			MongoBackend.store_event(veh4);
			System.out.println("Stored: " + veh4.getRegistrationNumber());
			MongoBackend.store_event(veh5);
			System.out.println("Stored: " + veh5.getRegistrationNumber());
			MongoBackend.store_event(veh6);
			System.out.println("Stored: " + veh6.getRegistrationNumber());
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}
}
